<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;


class Template extends Model
{

    

    

    // 表名
    protected $name = 'template';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    /**
     * 列表
     * @param $postData
     * @return array
     */
    public static function getList($postData)
    {
        $where = '1=1';
        $order = 'a.id desc';
        $page = isset($postData['page']) && !empty($postData['page']) ? intval($postData['page']) : 1;
        $limit = isset($postData['limit']) && !empty($postData['limit']) ? intval($postData['limit']) : 10;
        $start = ($page - 1) * $limit;
        $field = "a.*";

        $list = db('template')
            ->alias('a')
            ->where($where)
            ->order($order)
            ->limit($start, $limit)
            ->field($field)
            ->select();
        if (!empty($list)) {
            foreach ($list as &$v) {
                $v['createtimeText'] = isset($v['createtime']) && !empty($v['createtime']) ? date('Y-m-d H:i:s', $v['createtime']) : '';
                $v['full_url'] = request()->domain().$v['src'];
            }
            unset($v);
        }

        $total = db('template')
            ->alias('a')
            ->where($where)
            ->count();

        return [
            'list' => $list,
            'total' => $total,
        ];

    }

    /**
     * 新增
     * @param $postData
     * @return bool
     */
    public static function addRow($postData)
    {
        $data = [
            'name' => $postData['name'],
            'src' => $postData['src'],
            'createtime' => time(),
            'updatetime' => time(),
        ];

        Db::startTrans();
        try {
            Template::insert($data, true);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

    /**
     * 修改
     * @param $postData
     * @return bool
     */
    public static function updateRow($postData)
    {
        $data = [
            'name' => $postData['name'],
            'src' => $postData['src'],
            'updatetime' => time(),
        ];

        Db::startTrans();
        try {
            Template::update($data, ['id'=>$postData['id']]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

    /**
     * 删除
     * @param $postData
     * @return bool
     */
    public static function deleteRow($postData)
    {
        Db::startTrans();
        try {
            Template::where(['id'=>$postData['id']])->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

    /**
     * 所有
     * @return array
     */
    public static function getAll()
    {
        $where = '1=1';
        $order = 'a.id asc';
        $field = "a.id, a.name";

        $list = db('template')
            ->alias('a')
            ->where($where)
            ->order($order)
            ->field($field)
            ->select();

        return [
            'list' => $list,
        ];

    }





}
